package com.hulk.ratel.manage.commonutil;


import com.hulk.ratel.common.constant.enums.BussErrCode;
import com.hulk.ratel.manage.commoncache.ErrCodeConvertCache;
import com.hulk.ratel.manage.commonutil.bo.ErrcodeConvertBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by cmt on 2017/6/15.
 * ErrorConvertUtil
 */
@Slf4j
@Component
public class ErrorConvertUtil {

    @Autowired
    private ErrCodeConvertCache errCodeConvertCache;

    /**
     * 错误码转换
     *
     * @param platformId
     * @param payOrgCode
     * @param respCode
     * @return
     */
    public ErrcodeConvertBO convertErrCode(String respCode, String payOrgCode, String platformId) {
        List<ErrcodeConvertBO> errCodes = errCodeConvertCache.findErrCodeConvert(platformId, payOrgCode, respCode);
        ErrcodeConvertBO errorCode = errCodes != null && errCodes.size() > 0 ? errCodes.get(0) : null;
        if (errorCode == null) {
            log.error("支付机构返回错误码,在平台未找到对应册错误码：platformId:{},payOrgCode:{},respCode:{}", platformId, payOrgCode, respCode);
            ErrcodeConvertBO respErr = new ErrcodeConvertBO();
            respErr.setRespCode(BussErrCode.E_999999.getErrCode());
            respErr.setRespDesc(BussErrCode.E_999999.getErrDesc());
            return respErr;
        }
        return errCodes.get(0);
    }
}
